/*
 * Copyright 2014 Henrik Baerbak Christensen, Aarhus University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cs.rsa.ts14.india.statemachine;

import cs.rsa.ts14.framework.*;

/**
 * Define a ConcreteState for the LineSequenceState
 * 
 * @author Henrik Baerbak Christensen, Aarhus University
 * 
 */

public class EmptyState implements LineSequenceState {

	private final int MAX_EMPTY_LINE_COUNT = 2;

	private final int count;

	private final LineSequenceState previousNonCommentNonEmptyState;

	public EmptyState(LineSequenceState previousNonCommentNonEmptyState) {
		this(previousNonCommentNonEmptyState , 1 );
	}

	private EmptyState(LineSequenceState previousNonCommentNonEmptyState,
			int count) {
		this.previousNonCommentNonEmptyState = previousNonCommentNonEmptyState;
		this.count = count;
	}

	@Override
	public String lastError() {
		return "OK";
	}

	@Override
	public LineSequenceState transitionOn(LineType lineType) {
		LineSequenceState state = null;
		switch (lineType) {
		case EMPTY_LINE:
			if (count == MAX_EMPTY_LINE_COUNT) {
				state = new IllegalState("Illegal to have three consecutive empty lines.");
			} else {
				state = new EmptyState(previousNonCommentNonEmptyState, count + 1);
			}
			break;
		default:
			state = previousNonCommentNonEmptyState.transitionOn(lineType);
			break;
		}
		return state;
	}

}
